<?php
/**
 * Created by PhpStorm.
 * User: SQK
 * Date: 2018/8/14
 * Time: 14:55
 */
namespace app\blog\model;
use think\Model;
use think\Db;
class UserModel extends Model
{
    /**
     * 代理商记录
     */
    public function index($param){
        $where = array();
        if(!empty($param['mobile'])){
            $where['u.mobile'] = $param['mobile'];
        }
        if(!empty($param['only_incode'])){
            $where['u.only_incode'] = $param['only_incode'];
        }
        $result = $this->alias('u')
                        ->join('__AGENCY__ a','u.id=a.user_id')
                        ->join('__AGRATIO__ at','a.ag_level=at.id')
                        ->where($where)
                        ->field('u.id,u.mobile,u.only_incode,a.incode,a.brand_name,a.money,a.real_name,a.id_card,at.agr_level')
                        ->paginate(10);
        return $result;
    }
    /**
     *  代理商添加
     * return  1.添加失败 2.添加成功 3.非网站创建者不可创建超级管理员
     */
    public function addPost($date){
        //user表
        $arr1                    = array();
        $arr1['mobile']          = $date['mobile'];
        $arr1['sex']             = $date['sex'];
        $arr1['user_pass']       = cmf_password($date['user_pass']);
        $arr1['user_email']      = $date['user_email'];
        $arr1['user_nickname']       = '唯爱波比';
        $only_incode= guids();//推荐码
        $arr1['only_incode']     = $only_incode;
        //agency表
        $arr2       = array();
        $arr2['real_name']      = $date['real_name'];
        $arr2['id_card']        = $date['id_card'];
        $arr2['brand_name']     = $date['brand_name'];
        $arr2['ag_level']       = $date['agr_leve'];
        if(!empty($date['incode'])){
            $arr2['incode']     = $date['incode'];
        }
        //角色id
        $role_id                = $date['role_id'];
        Db::startTrans();
        $rest = $this->insertGetId($arr1);
        if($rest != false){
            $arr2['user_id'] = $rest;
            $arr2['ay_guid'] = guid();
            $rest1 = Db::name('agency')->insert($arr2);
            //添加激活码
            $dekeyid = $date['agr_leve'];//获取激活码数量
            $rest2   = Db::name('agratio')->where(['id'=>$dekeyid])->find();
            $num = $rest2['number'];
            $arra = array();
            for($i=0;$i<$num;$i++){
                $arra['dk_guid'] = guid();//唯一标识
                $arra['key_code'] = guidz();//激活码
                $arra['user_id'] = $rest;
                $arra['status'] = 2;
                $arra['c_time'] = time();
                $rest3 = Db::name('dekey')->insert($arra);
            }
            if($rest1 != false){
                if($role_id == 1){
                    Db::rollback();
                    return 3;
                }
                Db::name('RoleUser')->insert(["role_id" => $role_id, "user_id" => $rest]);
                Db::commit();
                return 2;
            }else{
                Db::rollback();
                return 1;
            }
        }else{
            return 1;
        }
    }
    /**
     * 删除代理商
    */
    public function del($id){
        //先删除激活码（dekey）->删除资金记录（balance表）->删除代理商配置信息（agency）->代理商记录（user表）
        Db::startTrans();
        //根据用户id删除激活码记录
        $rest1 = Db::name('dekey')->where(['user_id'=>$id])->delete();
        //根据用户id删除资金记录
        $rest2 = Db::name('balance')->where(['agency_id'=>$id])->delete();
        //根据用户id删除代理商配置并删除代理商
        $rest3 = Db::name('agency')->where(['user_id'=>$id])->delete();
        if($rest3 != false){
            $rest4 = Db::name('user')->where(['id'=>$id])->delete();
            if($rest4 != false){
                Db::commit();
                return 1;
            }else{
                Db::rollback();
                return 2;//删除失败
            }
        }else{
            Db::rollback();
            return 2;//删除失败
        }
    }
    /**
     * 获取资金记录
     */
    public function balance($param){
        $id = $param['id'];
        $where = array();
        if(!empty($param['status'])){
            $where['b.status'] = $param['status'];
        }
        if(!empty($param['start_time'])){
            $where['b.status'] = $param['status'];
        }
        if(!empty($param['end_time'])){
            $where['b.status'] = $param['status'];
        }
        //两个时间都输入
        if(!empty($param['start_time']) and  !empty($param['end_time']))
        {
            $where['b.c_time'] = ['between time', [$param['start_time'].' 00:00:00', $param['end_time'].' 23:59:59']]  ;
        }
        //输入start_time
        if(!empty($param['start_time']) and  empty($param['end_time'])){
            $where['b.c_time'] = ['>= time', $param['start_time'].' 00:00:00']  ;
        }
        //输入end_time
        if(empty($param['start_time']) and  !empty($param['end_time'])){
            $where['b.c_time'] = ['<= time', $param['end_time'].' 23:59:59']  ;
        }
        $result = Db::name('balance')->alias('b')
                                        ->where(['b.agency_id'=>$id])
                                        ->where($where)
                                        ->join('__AGENCY__ a','b.agency_id=a.user_id','left')
                                        ->field('b.*,a.real_name')
                                        ->paginate(10);
        return $result;
    }
    /**
     * 获取角色
     */
    public function role(){
        $rolesSrc = Db::name('role')->select();
        return $rolesSrc;
    }
    /**
     * 根据id获取用户的信息
    */
    public function getUsermsg($id){
        $result = $this->alias('u')
                        ->join('__AGENCY__ a','u.id=a.user_id')
                        ->where(['u.id'=>$id])
                        ->field('u.id,u.mobile,a.real_name')
                        ->find();
        return $result;
    }
}